<template>
	<view class="p-10 container">
		<scroll-view scroll-y="true" style="height: calc(100vh - 134px)">
			<view class="form-container">
				<uni-forms ref="form" :modelValue="objList" labelPosition="top" label-width="70vw">
					<view v-for="(item, index) in objList.listData" :key="index">
						<view class="hidden-title title">
							{{ item.teacherName}} {{ item.coursewareName ? `- ${item.coursewareName}` : "" }}
						</view>
						<view class="background-white">
							<template v-for="(itm, ind) in item.evaluateList">
								<uni-forms-item name="content">
									<template slot="label">
										<text>{{ itm.content }}</text>
									</template>
									<textarea v-model="itm.standard" class="text-area" :rows="5"
										placeholder-class="form-placeholder__style" disabled />
								</uni-forms-item>
								<uni-forms-item label="满意度" :name="['listData', index, 'evaluateList', ind, 'scoreId']"
									:rules="[{ required: true, errorMessage: '必填项不能为空' }]">
									<uni-data-checkbox v-model="itm.scoreId" :localdata="itm.scoreConfigList" />
								</uni-forms-item>
							</template>
							<uni-forms-item name="suggest" label="意见或者建议">
								<textarea style="height: 120rpx" v-model="item.suggest" class="text-area"
									placeholder-class="form-placeholder__style" placeholder="请输入" maxlength="100" />
							</uni-forms-item>
						</view>
					</view>
				</uni-forms>
			</view>
		</scroll-view>
		<view class="submit-group">
			<button class="u-button-cancel margin-left-xs" type="default" :ripple="true" @click="navigateBack">
				取消
			</button>
			<button class="u-button margin-right-xs" type="primary" :ripple="true" @click="submit">
				评价
			</button>
		</view>
	</view>
</template>

<script>
	import {
		getEvaluateDetail,
		submitEvaluate,
		reqDetailEvaluateList,
	} from "@/api/educational-training/trainingTasks";
	import {
		cloneDeep
	} from "lodash";
	export default {
		onLoad({
			id
		}) {
			this.id = id;
		},
		onShow() {
			this.initData();
		},
		onPullDownRefresh() {
			this.initData();
		},
		onReachBottom() {
			this.pageNum++;
			this.getlist();
		},

		data() {
			return {
				pageNum: 1,
				total: 0,
				id: "",
				objList: {
					listData: [],
				},
			};
		},
		methods: {
			initData() {
				this.pageNum = 1;
				this.total = null;
				this.getlist();
			},
			async getlist() {
				let res = await getEvaluateDetail(this.id);

				let evaluateList = [];
				const res2 = await reqDetailEvaluateList({
					trainPlanId: this.id
				});
				if (res2 && res2.data) {
					res2.data.standardConfigList.forEach((item) => {
						item.standardId = item.id;
						item.scoreId = "";
						const scoreConfigList = cloneDeep(res2.data.scoreConfigList);
						scoreConfigList.forEach((ite) => {
							ite.value = ite.id;
							ite.text = ite.typeLabel + "（" + ite.score + "）";
						});
						item.scoreConfigList = scoreConfigList;
						evaluateList.push(item);
					});
				}
				res.data.forEach((item) => {
					item.evaluateList = cloneDeep(evaluateList);
					item.trainPlanId = this.id;
				});
				this.objList.listData = res.data;

				uni.stopPullDownRefresh();
			},
			async submit() {
				await this.$refs.form.validate();

        const data = []
        this.objList.listData.map(d=>{
          let item = {}
          item.coursewareHistoryId = d.coursewareHistoryId
          item.suggest = d.suggest
          item.teacherId = d.teacherId
          item.evaluateList = d.evaluateList.map(ev=>{
            return { scoreId: ev.scoreId, standardId: ev.standardId}
          })
          data.push(item)
        })
				await submitEvaluate(this.id, data);
				uni.showToast({
					title: "提交成功！",
					icon: "success",
				});
				setTimeout(() => {
					uni.navigateBack();
				}, 1000);
			},
			navigateBack() {
				uni.navigateBack();
			},
		},
	};
</script>

<style lang="scss">
	.submit-group {
		background: #fff;
		z-index: 9;
		padding: 10px 30px;
	}

	.title {
		font-weight: bold;
		color: black;
	}

	.button-wrap {
		padding: 30rpx 30rpx 30rpx;
		display: flex;

		.space {
			width: 50rpx;
		}

		.submit-button,
		.draft-button {
			flex: 1;
			color: #ffffff;
			line-height: 88rpx;
			text-align: center;
			border-radius: 50rpx;
			font-size: 36rpx;
		}

		.submit-button {
			background: #606576;
		}

		.draft-button {
			background: #2ac0c8;
		}
	}
</style>
